package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SpringLayout;

import utility.Record;

import business.SalleBL;
import business.SeanceBL;
import business.ServiceBL;
import business.TypeEnseignementBL;


public class FrameAjoutSeance extends JFrame {
	
	//champs du formulaire
	private JPanel general;
    private JButton save;
    private JButton annuler;
	private JComboBox comboSalle;
	private JComboBox comboService;

  //variables d'affichage du formulaire
	private int idSeance = -1;
    private int idService;
    private int idSalle = -1;
    private int idPeriode;
    private boolean modifier;
	private SemaineUI semaineUI;
        
	//methodes de récupperation des éléments des champs
	public int getId(){
		return idService;
	}
    
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	

	private JComboBox makeComboSalle (SalleBL blSalle){
		JComboBox comboTEns = new JComboBox();
		for (Record typeEns : blSalle.getListeSalle()) {
			//On va selectionner le statut qui correspond au statut de la base
			if(typeEns.getId() == idSalle){
				comboTEns.addItem(typeEns);
				comboTEns.setSelectedItem(typeEns);
			} else {
				comboTEns.addItem(typeEns);
			}
		}
		return comboTEns;
	}

	private JComboBox makeComboService (ServiceBL blService){
		JComboBox comboTEns = new JComboBox();
		for (Record typeEns : blService.getListeService()) {
			System.out.println("ID : "+typeEns.getLibelle());
			//On va selectionner le statut qui correspond au statut de la base
			if(typeEns.getId() == idService){
				comboTEns.addItem(typeEns);
				comboTEns.setSelectedItem(typeEns);
			} else {
				comboTEns.addItem(typeEns);
			}
		}
		return comboTEns;
	}

	public FrameAjoutSeance(int idSean, int idServ, int idPer) {
		
		idSeance = idSean;
		idService = idServ;
		idPeriode = idPer;
		
		System.out.println("seance: "+ idSean+" service: "+idServ+" periode: "+idPer);
		
		SalleBL blSalle = new SalleBL();
		ServiceBL blService = new ServiceBL();
		
		
    	setSize(500, 300);
		setTitle("Ajouter une séance");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder("Paramètres"),
		BorderFactory.createEmptyBorder(5,5,5,5)));

        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
/*        JLabel labelNom = new JLabel("Service:", JLabel.TRAILING);
        p.add(labelNom);
        if (idService != -1) {
        	comboService = new JComboBox();
        }
        else {
        	comboService = makeComboService (blService);
        }
        labelNom.setLabelFor(comboService);
        p.add(comboService);
*/
        JLabel labelService = new JLabel("Salle:", JLabel.TRAILING);
        p.add(labelService);
		comboSalle = makeComboSalle (blSalle);
        labelService.setLabelFor(comboSalle);
        p.add(comboSalle);
        
        //Lay out the panel
        SpringUtilities.makeCompactGrid(p,
                                        1, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new FrameAjoutSeanceListener());
        commandBox.add(save);
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new FrameAjoutSeanceListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);

	}
	
	public FrameAjoutSeance(SemaineUI semaineUI, int idSean, int idServ, int idPer, boolean modifier) {
		this.semaineUI = semaineUI;
		this.modifier = modifier; 
		idSeance = idSean;
		idService = idServ;
		idPeriode = idPer;
		
		if (idSeance != -1) {
			SeanceBL seanceBL = new SeanceBL(idSeance);
			idSalle = seanceBL.getIdSalle();
		}
		
		System.out.println("seance: "+ idSean+" service: "+idServ+" periode: "+idPer);
		
		SalleBL blSalle = new SalleBL();
		ServiceBL blService = new ServiceBL();
		
		
    	setSize(500, 300);
		setTitle("Ajouter une séance");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder("Paramètres"),
		BorderFactory.createEmptyBorder(5,5,5,5)));

        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
/*        if (modifier == false) {
        JLabel labelNom = new JLabel("Service:", JLabel.TRAILING);
        p.add(labelNom);
        	comboService = new JComboBox();
        }
 */       if (modifier == true) {
            JLabel labelNom = new JLabel("Service:", JLabel.TRAILING);
            p.add(labelNom);
        	comboService = makeComboService (blService);
        	labelNom.setLabelFor(comboService);
        	p.add(comboService);
        }

        JLabel labelService = new JLabel("Salle:", JLabel.TRAILING);
        p.add(labelService);
		comboSalle = makeComboSalle (blSalle);
        labelService.setLabelFor(comboSalle);
        p.add(comboSalle);
        
        //Lay out the panel
        int size;
        if (modifier == true)
        	size = 2;
        else
        	size = 1;
        SpringUtilities.makeCompactGrid(p,
                                        size, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new FrameAjoutSeanceListener());
        commandBox.add(save);
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new FrameAjoutSeanceListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);

	}

	public int getIdSalle() {
		return ((Record)comboSalle.getSelectedItem()).getId();
	}

	public int getIdService() {
		return ((Record)comboService.getSelectedItem()).getId();
	}

	private class FrameAjoutSeanceListener implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
			System.out.println("Action");		
			if (e.getActionCommand().equals("Enregistrer")) {
				System.out.println("Demande d'enregistrement");
				SeanceBL seanceSave = new SeanceBL();
				// si -1, c'est un ajout d'un type heure, sinon modification du
				// type heure de l'id concerné
				
				//Ajout d'une nouvelle seance avec service variable
				if (idSeance == -1 && modifier == true) {
					seanceSave.ajouterSeance(idSeance, idPeriode, getIdSalle(), getIdService());
				} 
				//Ajout d'une nouvelle seance avec service fixe
				if (idSeance == -1 && modifier == false) {
					seanceSave.ajouterSeance(idSeance, idPeriode, getIdSalle(), idService);
				} 
				//Modification d'une seance avec service fixe
				if (idSeance != -1 && modifier == false) {
					seanceSave.modifierSeance(idSeance, idPeriode, getIdSalle(), idService);
				} 
				//Modification d'une seance avec service variable
				if (idSeance != -1 && modifier == true) {
					seanceSave.modifierSeance(idSeance, idPeriode, getIdSalle(), getIdService());
				} 
				semaineUI.load();
				dispose();
			} else if (e.getActionCommand().equals("Annuler")) {
				System.out.println("annulation");
				dispose();
			}
		}
	}

}
